﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
   public class KhachHangDAO
    {
       public List<KhachHangDTO> DanhSachKhachHang()
       {
           List<KhachHangDTO> kq = new List<KhachHangDTO>();
           DataProvider dp = new DataProvider();
           string strsql = "select*from KhachHang";
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           if (dr.HasRows)
           {
               while (dr.Read())
               {
                   KhachHangDTO kh = new KhachHangDTO();
                   kh.CoQuan = dr["CoQuan"].ToString();
                   kh.DiaChi = dr["DiaChi"].ToString();
                   kh.DienThoai = dr["DienThoai"].ToString();
                   kh.GioiTinh = dr["GioiTinh"].ToString();
                   kh.KH_ID = int.Parse(dr["KH_ID"].ToString());
                   kh.MaKhachHang = dr["MaKhachHang"].ToString();
                   kh.TenKhachHang = dr["TenKhachHang"].ToString();
                   kq.Add(kh);
               }
           }
           dp.Disconnect();

           return kq;
       }
       public KhachHangDTO LayKhachHangByID(int kH_ID)
       {
           KhachHangDTO kh = new KhachHangDTO();
           DataProvider dp = new DataProvider();
           string strsql = "select*from KhachHang where KH_ID="+kH_ID;
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           dr.Read();
           kh.CoQuan = dr["CoQuan"].ToString();
           kh.DiaChi = dr["DiaChi"].ToString();
           kh.DienThoai = dr["DienThoai"].ToString();
           kh.GioiTinh = dr["GioiTinh"].ToString();
           kh.KH_ID = int.Parse(dr["KH_ID"].ToString());
           kh.MaKhachHang = dr["MaKhachHang"].ToString();
           kh.TenKhachHang = dr["TenKhachHang"].ToString();
           dp.Disconnect();
           return kh;
       }
       public KhachHangDTO LayKhachHangByMaKhachHang(string MaKhachHang)
       {
           KhachHangDTO kh = new KhachHangDTO();
           DataProvider dp = new DataProvider();
           string strsql = "select*from KhachHang where MaKhachHang='" + MaKhachHang+"'";
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           dr.Read();
           kh.CoQuan = dr["CoQuan"].ToString();
           kh.DiaChi = dr["DiaChi"].ToString();
           kh.DienThoai = dr["DienThoai"].ToString();
           kh.GioiTinh = dr["GioiTinh"].ToString();
           kh.KH_ID = int.Parse(dr["KH_ID"].ToString());
           kh.MaKhachHang = dr["MaKhachHang"].ToString();
           kh.TenKhachHang = dr["TenKhachHang"].ToString();
           dp.Disconnect();
           return kh;
       }
       public Boolean ThemKhachHang(KhachHangDTO kh)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "";
           dp.Connect();
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;

           dp.Disconnect();

           return kq;
       }
       public Boolean CapNhatKhachHang(KhachHangDTO kh)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "";
           dp.Connect();
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;

           dp.Disconnect();

           return kq;
       }
       public Boolean XoaKhachHangByID(int kH_ID)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "delete from KhachHang where KH_ID="+kH_ID;
           dp.Connect();
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;
           dp.Disconnect();
           return kq;
       }
       public Boolean XoaKhachHangByMaKhachHang(string MaKhachHang)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "delete from KhachHang where MaKhachHang='"+MaKhachHang+"'";
           dp.Connect();
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;
           dp.Disconnect();
           return kq;
       }


    }
}
